<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use common\models\News;
use common\models\Alert;
use common\models\Command;
use common\models\FileAlert;
use common\models\IPAlert;
use common\models\URLAlert;
use common\models\UserLog;
use common\models\Config;
use common\models\Sensor;
use common\models\User;

/**
 * Task controller
 */
class TaskController extends Controller {

    /**
     * index Task
     *
     * @return 0
     */
    public function actionIndex() {
        return 0;
    }

    /**
     * every-day Task
     *
     * @return 0
     */
    public function actionEveryDay() {
        $this->actionSetMachineCode();
        $this->cleanData();
        $this->licenseNews();
        return 0;
    }

    public function actionSetMachineCode() {
        $shell = 'sudo dmidecode |grep -A10 \'System Information$\' | grep \'Serial Number\'';
        exec($shell, $res);
        $code = md5($res[0]);
        Yii::$app->cache->set('MachineCode', $code);
        return 0;
    }

    private function cleanData() {
        $time = mktime(0, 0, 0, date("m"), date("d"), date("Y") - 1);
        $alertIdList = Alert::find()->select('id')->where(['<', 'created_at', $time])->asArray()->all();
        $alertIdList = array_column($alertIdList, 'id');
        Alert::deleteAll(['id' => $alertIdList]);
        Command::deleteAll(['<', 'created_at', $time]);
        FileAlert::deleteAll(['AlertID' => $alertIdList]);
        IPAlert::deleteAll(['AlertID' => $alertIdList]);
        URLAlert::deleteAll(['AlertID' => $alertIdList]);
        UserLog::deleteAll(['<', 'created_at', $time]);
        return 0;
    }

    private function licenseNews() {
        $license = Config::getLicense();
        $sensorNowMaxCount = $license['sensorNowCount'];
        $sensorNowCount = (int) Sensor::find()->where(['status' => [Sensor::ON_LINE, Sensor::OFF_LINE]])->count();
        $userList = User::find()->where(['role' => 'admin'])->all();
        if ($sensorNowCount > $sensorNowMaxCount) {
            $news = new News();
            $news->type = News::TYPE_OVERRUN;
            $news->title = '已经超过保护上限';
            $news->content = '计算机数量已经超过授权保护计算机数量上限，点击<a href="/seting/license">[许可证]</a>查看详情';
            $news->send($userList);
        } elseif ($sensorNowCount > ($sensorNowMaxCount * 0.85)) {
            $news = new News();
            $news->type = News::TYPE_OVERRUN;
            $news->title = '即将超过保护上限';
            $news->content = '计算机数量已经超过授权保护计算机数量的85%，点击<a href="/seting/license">[许可证]</a>查看详情';
            $news->send($userList);
        } 
//        elseif ($sensorNowCount == 0) {
//            $news = new News();
//            $news->type = News::TYPE_OVERRUN;
//            $news->title = '无有效许可证';
//            $news->content = '系统中找不到有效的许可证，点击<a href="/seting/license">[许可证]</a>查看详情';
//            $news->send($userList);
//        }
        // foreach ($license['list'] as $SN => $details) {
        //     if($details['status'] == '已过期'){
        //         $news = new News();
        //         $news->type = News::TYPE_OVERDUE;
        //         $news->title = '许可证过期';
        //         $news->content = '序列号为“'.$SN.'”的许可证过期，点击<a href="/seting/license">[许可证]</a>查看详情';
        //         $news->send($userList);
        //     }
        // }
        return 0;
    }

}
